package com.mes.controller;

import com.yy.base.common.action.BaseCtrl;
import com.yy.base.common.dao.Dao;
import com.yy.base.page.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 看板数据展示
 */
@Controller
@RequestMapping(value="/kanban")
public class KanbanController extends BaseCtrl {
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	private Dao dao;
	
	@ResponseBody
	@RequestMapping(value="/datasource.html")
	public String quality(HttpServletRequest request, HttpServletResponse response) {
		return "quality";
	}
	
	@ResponseBody
	@RequestMapping(value="/getKanbanInfo.html")
	public Message getKanbanInfo(HttpServletRequest request, HttpServletResponse response) {
		Message message = new Message();
		try {
			String ip = (String)request.getSession().getAttribute("requestIP");
			String sql = "select * from emes_kanban_datasource where ip=?";
			Map<String, Object> map = dao.queryUnique(sql, ip);
			if (map == null) {
				sql = "select * from emes_kanban_datasource where is_Default='Y'";
				map = dao.queryUnique(sql);

				if (map == null) {
					message.setErrorMessage("此IP未设置数据源！");
					return message;
				}
			}
			message.setData(map);
			message.setOkMessage("查询看板数据源成功！");
		} catch (Exception e) {
			e.printStackTrace();
			message.setErrorMessage("查询看板数据源异常：" + e.getMessage());
		}
		return message;
	}
	@ResponseBody
	@RequestMapping(value="/getKanbanMessage.html")
	public Message getKanbanMessage(HttpServletRequest request, HttpServletResponse response) {
		Message message = new Message();
		try {
			String sql = "select ekm.title,ekm.Text,ekm.Description "+
						 "  from emes_kanban_message_ip ekmi, "+
						 "	       Eam_Facilities        ef, "+
						 "	       emes_kanban_message    ekm "+
						 "	 where ef.type = 'KANBAN' "+
						 "	   and ef.Eam_Facilities_id = ekmi.Eam_Facilities_id "+
						 "	   and ef.ip1 = ? "+
						 "	   and ekmi.emes_kanban_message_id = ekm.emes_kanban_message_id "+
						 "	   and ekm.timefrom < now() "+
						 "	   and ekm.timeto > now()";
			
			String ip = request.getParameter("ip");
			
			List<Map<String, Object>> list = dao.query(sql,ip);
			message.setData("list", list);

			message.put("sysDate", dateFormat.format(new Date()));
			message.setOkMessage("查询看板数据源成功！");
		} catch (Exception e) {
			message.setErrorMessage("查询看板数据源异常：" + e.getMessage());
		}
		return message;
	}
}